 $(function(){
	$('#'+tabId+' #reportDrawCharts_id').on('click',function(){
		var param = jQuery.extend(true,{},paramTabPage[tabId]);
		param.dateFrom = $('#'+tabId+' #sdate').val();
		param.dateTo=$('#'+tabId+' #edate').val();
		param.stdyear=$('#'+tabId+' #benchmarkYearID').val();
		param.groupName=$('#'+tabId+' #groupversionID').val();
		openTab('/pages/simpreport/reportDrawCharts.jsp', "分析报告自定义绘制",'reportDrawCharts_tab', 'reportDrawCharts_tab',param);
	})
})
var iscreate=0;//检测是否生成图片完成
function getNowTime() {
	var d = new Date()
	var vYear = d.getFullYear()
	var vMon = d.getMonth() + 1
	var vDay = d.getDate()
	var h = d.getHours();
	var m = d.getMinutes();
	var se = d.getSeconds();
	s = vYear + (vMon < 10 ? "0" + vMon : vMon)+ (vDay < 10 ? "0" + vDay : vDay) + (h < 10 ? "0" + h : h)+ (m < 10 ? "0" + m : m) + (se < 10 ? "0" + se : se);
	return s
}
// 医院级别的选择
$('.form-level  label').on('click', function() {
	$('.form-level  label').siblings("label").removeClass('select');
	$(this).addClass('select');
	$('.form-level input').removeAttr('checked');
	var id = $(this).attr('for');
	$('#' + id).attr('checked', 'checked');
});
//医院类型的选择
$('.form-level-report-type label').on('click', function(){
    $('.form-level-report-type label').siblings("label").removeClass('select');
    $(this).addClass('select');
    $('.form-level-report-type input').removeAttr('checked');
    var id=$(this).attr('for');
    $("#"+tabId+"# "+ id).attr('checked','checked');
});
$("#"+tabId+" #generateBtnAR").on("click",function() {
	loadTabParam(tabId);
	var param = jQuery.extend(true,{},paramTabPage[tabId]);
	var hospCode=param.hospCode; //医院院区编码
/*	var isBlank = false;
	$.each($('[required="required"]'), function(i, n) {
		if ($(n).val() == '') {
			setTimeout(function() {
				$(n).removeClass('ue-shake');
			}, 900);
			$(n).addClass('ue-shake');
			isBlank = true;
		}
	});
	if (isBlank)
		return;*/

	$("#"+tabId+" #generateBtnAR").attr('disabled', 'disabled').find('i').addClass('fa-spin');
	$('#exportResult .panel-footer1').html('<i class="fa fa-cog fa-spin fa-4x fa-fw margin-bottom text-success" style="margin-left:190px;" ></i>');
	var dateFrom = $('#'+tabId+' #sdate').val();
	var dateTo = $('#'+tabId+' #edate').val();
	var hospBuildYear = $('#'+tabId+' #hospBuildYear').val();
	var hospName=$('#'+tabId+' #hospName').val();
//	var benchmarkYear=$('#'+tabId+' #benchmarkYearID').val();
//	var groupVersion=$('#'+tabId+' #groupversionID').val();
	var hospLevel= $('input[name="hospLevel"]:checked ').val();
	var hospType= $('input[name="hospType"]:checked ').val();
	var hospBedNum=$('#'+tabId+' #hospBedNum').val();
	var hospRealBedNum=$('#'+tabId+' #hospRealBedNum').val();
	var hospCaseNum=$('#'+tabId+' #hospCaseNum').val();
	var hospDocNum=$('#'+tabId+' #hospDocNum').val();
	var hospBedDaysNum=$('#'+tabId+' #hospBedDaysNum').val();
	var specCode=$('#'+tabId+' #specCodeID').val();
	var specDesc=$('#'+tabId+' #specCodeID').text();
	var isInmedical= $("#"+tabId+" #specCodeId .radio_bg_check input").val();
	var isComplete= $("#"+tabId+" #isCompleteId .radio_bg_check input").val();
	var mdcCode=$('#'+tabId+' #mdcCode').val();
	var drgCodes=$('#'+tabId+' #drgCodes').val();
	
	// 导出基础数据和表格信息
	console_wt('获取分析报告基础数据...');
		$.ajax({type : "post",
				url : contextPath+ "/BZanalysisDRGsReport/findAnalysisReportInfo.dhc",
				data : {
					"dateFrom":dateFrom ,
					"dateTo": dateTo ,
//					"benchmarkYear": benchmarkYear,
					"hospName":hospName,
					"hospCode":hospCode,
					"hospBuildYear":hospBuildYear,
					"hospLevel":hospLevel,
					"hospType":hospType,
					"hospBedNum":hospBedNum,
					"hospRealBedNum":hospRealBedNum,
					"hospCaseNum":hospCaseNum,
					"hospDocNum":hospDocNum,
					"hospBedDaysNum":hospBedDaysNum,
					"specCode":specCode,
					"specDesc":specDesc,
					"isInmedical":isInmedical,
					"isComplete":isComplete,
//					"groupVersion":groupVersion,
					"mdcCode":mdcCode,
					"drgCodes":drgCodes
				},
				success : function(result) {
					if (result.data.errorMessageData == undefined) {
						
						if (result.data.newdrgs == undefined|| result.data.newdrgs != 1) {
							var data = result.data;
							$("#t2").show();
							createPicture(data);
						
							var timer=setInterval(function(){
							    if (iscreate=='1') {
							     clearInterval(timer);
								$.ajax({   
									type : "post",
									url : contextPath+ "/BZanalysisDRGsReport/createAnalysisReport.dhc",
									data : {"isInmedical":isInmedical },
									success : function(result) {
										if(result.success){
											var patch=result.data.patch;
											var hospname=result.data.hospname;
											var exportBtnHtml = '<div class="btn-group" role="group" style="margin-left: 150px;">';
											exportBtnHtml += '<a type="button" download="'+ hospname+ '住院病例DRGs分析报告.docx" class="btn btn-default btn-lg" href="'+ patch+ '"><i class=" icon-save"></i>&nbsp;下载分析报告</a>';														
											exportBtnHtml += '</div>';
											$("#"+tabId+" #generateBtnAR").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult .panel-footer1').html(exportBtnHtml);
											$('#'+tabId+' #successful').message({
														type : 'success',
														content : '分析报告生成成功！'
												});	
										}else{
											var exportBtnHtml =result.errorMessage;
											$("#"+tabId+" #generateBtnAR").removeAttr('disabled','disabled').find('i').removeClass('fa-spin');
											$('#exportResult .panel-footer1').html(exportBtnHtml);
											$('#'+tabId+' #successful').message({
														type : 'error',
														content : result.errorMessage
												});		
										}
									}
								});
							} 
							   else {
								$("#"+tabId+" #generateBtnAR").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
								$('#exportResult .panel-footer1').html('<p class="text-danger">'+ data.errorMessage+ '</p>');
							}
							},1000)
						} else {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							   exportBtnHtml +='<table class="newGroup" style="line-height:26px">';
							   exportBtnHtml +='<caption style="color:red">新增病组：</caption>';
							for(var i=0;i<result.data.drgsList.length;i+=2){
								if(i == result.data.drgsList.length-1){
									exportBtnHtml += '<tr><td>'+ result.data.drgsList[i].PARDRG_CODE+ '</td><td>----</td><td>'+result.data.drgsList[i].PARDRG_CNT+'例</td></tr>';
								}else{
									exportBtnHtml += '<tr><td>'+ result.data.drgsList[i].PARDRG_CODE+ '</td><td>----</td><td>'+result.data.drgsList[i].PARDRG_CNT+'例</td><td>&nbsp;</td><td>'+ result.data.drgsList[i+1].PARDRG_CODE+ '</td><td>----</td><td>'+result.data.drgsList[i+1].PARDRG_CNT+'例</td></tr>';
								}
							}
							exportBtnHtml += '</table>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateBtnAR").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer1').html(exportBtnHtml);
							$('#'+tabId+' #successful').message({
								type : 'error',
								content : '本院含有新增病组，请到标杆测算页面测算标杆并保存！'
							});
						}
					} else {
						if (result.data.errorMessageData != undefined) {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">'+ result.data.errorMessageData+ '</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateBtnAR").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer1').html(exportBtnHtml);
							$('#'+tabId+' #successful').message({
												type : 'error',
												content : '"'+ result.data.errorMessageData+ '"'
											});
						} else {
							var exportBtnHtml = '<div class="btn-group" role="group">';
							exportBtnHtml += '<span style="color:red;font-size:10px;">后台数据报错:'+result.data.errorMsg+'！</span>';
							exportBtnHtml += '</div>';
							$("#"+tabId+" #generateBtnAR").removeAttr('disabled', 'disabled').find('i').removeClass('fa-spin');
							$('#exportResult .panel-footer1').html(exportBtnHtml);
							$('#'+tabId+' #successful').message({
								type : 'error',
								content : '后台数据报错:'+result.data.errorMsg
							});
						}
					}
				}
			});
});
// 生成分析报告所需图表
function createPicture(data) {
        iscreate=0;
	console_wt('开始生成图表...');
	console_wt('构建手术非手术分布图...');
	isOperChartData = [];
	 if(data.operCnt!=0){
		 isOperChartData.push({name : '手术',value : data.operCnt})
	   }
	 if(data.noOperCnt!=0){
		 isOperChartData.push({name : '非手术',value : data.noOperCnt})
		   }
	 analysisReportPieCharts(true,50,'picone',$('#imageOne'), isOperChartData, "", "");
	
	console_wt('构建手术级别分布图...');
	chartData = [];
	 if(data.levOne!=0){
	    chartData.push({name : '一级',value : data.levOne})
	   }
	 if(data.levTwo!=0){
		    chartData.push({name : '二级',value : data.levTwo})
		   }
	 if(data.levThree!=0){
		    chartData.push({name : '三级',value : data.levThree})
		   }
	 if(data.levFour!=0){
		    chartData.push({name : '四级',value : data.levFour})
		   }
	 if(data.levOther!=0){
		    chartData.push({name : '其他',value : data.levOther})
		   }
	 analysisReportPieCharts(true,50,'pictwo',$('#imageTwo'), chartData, "", "");
	 
	 console_wt('构建各MDC专业本期和同期出院病例数分布图...');	
	 var xData = [];
	 var yData = [];
	 var yData1 = [];
	 var MdcCntList=data.MdcCntList
	 for (var i = 0; i < MdcCntList.length; i++) {	
		 xData[i]=MdcCntList[i].MDC;
		 yData[i]=MdcCntList[i].THISYEAR;
		 yData1[i]=MdcCntList[i].LASTYEAR;
	 }
	 analysisReportTranBarChartsbz('picthree',$('#imageThree'),xData,yData,yData1);
	
	 console_wt('构建DRGs病组复杂程度分布图...');
		chartData = [];
		 if(data.DRGsType1Num!=0){
			    chartData.push({name : '伴重要合并症和伴随病',value : data.DRGsType1Num})
			   }
		 if(data.DRGsType3Num!=0){
			    chartData.push({name : '伴一般合并症和伴随病',value : data.DRGsType3Num})
			   }
		 if(data.DRGsType5Num!=0){
			    chartData.push({name : '不伴合并症和伴随病',value : data.DRGsType5Num})
			   }
		 if(data.DRGsType9Num!=0){
			    chartData.push({name : '其他',value : data.DRGsType9Num})
			   }
		analysisReportPieCharts(true,31,'picfour',$('#imageFour'), chartData, "", "");

		console_wt('构建DRGs病例复杂程度分布图...');
		chartData = [];
		 if(data.CaseType1Num!=0){
			    chartData.push({name : '伴重要合并症和伴随病',value : data.CaseType1Num})
			   }
		 if(data.CaseType3Num!=0){
			    chartData.push({name : '伴一般合并症和伴随病',value : data.CaseType3Num})
			   }
		 if(data.CaseType5Num!=0){
			    chartData.push({name : '不伴合并症和伴随病',value : data.CaseType5Num})
			   }
		 if(data.CaseType9Num!=0){
			    chartData.push({name : '其他',value : data.CaseType9Num})
			   }
		 analysisReportPieCharts(true,31,'picfive',$('#imageFive'), chartData, "", "");

		console_wt('构建各手术科室技术难度（CMI值）和医疗产出（总权重）象限分布图...');
		chartData = [];
			var xyAxis = {"xAxis": {"code": "CMI", "desc": "CMI"}, "yAxis": {"code": "totalWeight", "desc": "总权重"}}
			if(data.operDepMapCWList.length!=0){
				for (var i = 0; i < data.operDepMapCWList.length; i++) {
					chartData.push({
						name:data.operDepMapCWList[i].depName,
						CMI:data.operDepMapCWList[i].CMI,
						totalWeight:data.operDepMapCWList[i].totalWeight
					});
				}
			}else {
				chartData.push({
					name:"-",
					CMI:0,
					totalWeight:0
				});
			}
		analysisReportScatterChart(true,'picsix', $('#imageSix'), xyAxis, chartData, "","","",""); 
		
		console_wt('构建各非手术科室技术难度（CMI值）和医疗产出（总权重）象限分布图...');
		chartData = [];
		var xyAxis = {"xAxis": {"code": "CMI", "desc": "CMI"}, "yAxis": {"code": "totalWeight", "desc": "总权重"}}
		if(data.noOperDepMapCWList.length!=0){
			for (var i = 0; i < data.noOperDepMapCWList.length; i++) {
				chartData.push({
					name:data.noOperDepMapCWList[i].depName,
					CMI:data.noOperDepMapCWList[i].CMI,
					totalWeight:data.noOperDepMapCWList[i].totalWeight
				});
			}
		}else {
			chartData.push({
				name:"-",
				CMI:0,
				totalWeight:0
			});
		}
	   analysisReportScatterChart(true,'picseven', $('#imageSeven'), xyAxis, chartData, "","","",""); 
		
		console_wt('构建各手术科室费用和时间消耗指数象限分布图...');
		chartData = [];
			var xyAxis = {"xAxis": {"code": "timeEff", "desc": "时间消耗指数"}, "yAxis": {"code": "feeEff", "desc": "费用消耗指数"}}
			if(data.operDepMapEFList.length!=0){
				for (var i = 0; i < data.operDepMapEFList.length; i++) {
					chartData.push({
						name:data.operDepMapEFList[i].depName,
						timeEff:data.operDepMapEFList[i].timeEff,
						feeEff:data.operDepMapEFList[i].feeEff
					});
				}
			}else {
				chartData.push({
					name:"-",
					timeEff:0,
					feeEff:0
				});
			}
		analysisReportScatterChart(true,'piceight', $('#imageEight'), xyAxis, chartData, "","",1,1); 
		
		console_wt('构建各非手术科室费用和时间消耗指数象限分布图...');
		chartData = [];
			var xyAxis = {"xAxis": {"code": "timeEff", "desc": "时间消耗指数"}, "yAxis": {"code": "feeEff", "desc": "费用消耗指数"}}
			if(data.operDepMapEFList.length!=0){
				for (var i = 0; i < data.noOperDepMapEFList.length; i++) {
					chartData.push({
						name:data.noOperDepMapEFList[i].depName,
						timeEff:data.noOperDepMapEFList[i].timeEff,
						feeEff:data.noOperDepMapEFList[i].feeEff
					});
				}
			}else {
				chartData.push({
					name:"-",
					timeEff:0,
					feeEff:0
				});
			}
		analysisReportScatterChart(true,'picnine', $('#imageNine'), xyAxis, chartData, "","",1,1); 
		
		 console_wt('构建各MDC总权重分布图...');	
		 var xData = [];
		 var yData = [];
		 var yData1 = [];
		 var MdcCntList=data.MdcCntList
		 for (var i = 0; i < MdcCntList.length; i++) {	
			 xData[i]=MdcCntList[i].MDC;
			 yData[i]=MdcCntList[i].TOTALWEIGHT;
		 }
		 analysisReportTranBarChartsbz('picten',$('#imageTen'),xData,yData,yData1);
	
		console_wt('构建各MDC总权重和CMI象限分布图...');
		chartData = [];
			var xyAxis = {"xAxis": {"code": "CMI", "desc": "CMI"}, "yAxis": {"code": "totalWEIGHT", "desc": "总权重"}}
			for (var i = 0; i < data.MDCnormalWeightAndCMIList.length; i++) {
				chartData.push({
					name:data.MDCnormalWeightAndCMIList[i].CODE,
					CMI:data.MDCnormalWeightAndCMIList[i].CMI,
					totalWEIGHT:data.MDCnormalWeightAndCMIList[i].WEIGHT
				});
			}
			var CMIMid=data.CMImid;
			var weightMid=data.totalWeightmid;
		analysisReportScatterChart(true,'piceleven', $('#imageEleven'), xyAxis, chartData, "","",weightMid,CMIMid); 

	iscreate=1;
	console_wt('图表构建完毕！');
	return true;
}
function console_wt(s) {
	$('#exportResult .panel-footer1').append('<p style="text-align:center;">' + s + '</p>');
}